//存放主要交换逻辑js代码
//javascript模块化
//seckill.detail.init(params);
var seckill = {
    //封装秒杀相关ajax的url
    URL: {
        now: function () {
            return '/seckill/time/now';
        },
        exposer: function (seckillId) {
            return '/seckill/' + seckillId + '/exposer';
        },
        execution: function (seckillId, md5) {
            return '/seckill/' + seckillId + '/' + md5 + '/execution';
        }
    },
    handleSeckillKill: function (seckillId, node) {
        //获取秒杀地址，处理秒杀逻辑，执行秒杀
        node.hide().html('<button class="btn btn-primary btn-lg" id="killBtn">开始秒杀</button>');
        $.post(seckill.URL.exposer(seckillId), {}, function (result) {
            //在回调函数中，执行交互流程
            if (result && result['success']) {
                var exposer = result['data'];
                if (exposer['exposed']) {
                    //开启秒杀
                    //获取秒杀地址
                    var md5 = exposer['md5'];
                    var killUrl = seckill.URL.execution(seckillId, md5);
                    console.log('killUrl:' + killUrl);
                    //不用click，这个是永久绑定
                    //绑定一次点击事件
                    $('#killBtn').one('click', function () {
                        //执行秒杀请求
                        //1.先禁用按钮
                        $(this).addClass('disable');
                        //2.发送秒杀请求
                        $.post(killUrl, {}, function (result) {
                            if (result && result['success']) {
                                var killResult = result['data'];
                                var state = killResult['state'];
                                var stateInfo = killResult['stateInfo'];
                                //3.显示秒杀结果
                                node.html('<span class="label label-success">' + stateInfo + '</span>');
                            }
                        });
                    });
                    node.show();
                } else {
                    //未开启秒杀，用户客户端计时太快
                    var now = exposer['now'];
                    var start = exposer['start'];
                    var end = exposer['end'];
                    //重新计算计时逻辑
                    seckill.countdown(seckillId, now, start, end);
                }
            } else {
                console.log("result:" + result)
            }
        })
    }
    ,
    //验证手机号 isNaN(phone)数字验证
    validatePhone: function (phone) {
        if (phone && phone.length == 11 && !isNaN(phone)) {
            return true;
        } else {
            return false;
        }
    }
    ,
    //计时服务
    countdown: function (seckillId, nowTime, startTime, endTime,localTime) {
        var seckillBox = $('#seckill-box');
        var timeDiff = localTime-nowTime;
        //时间判断
        console.log(startTime-nowTime);
        if (nowTime > endTime) {
            //秒杀结束
            seckillBox.html('秒杀已结束');

        } else if (nowTime < startTime) {
            //秒杀未开始，计时事件绑定
            var killTime = new Date(startTime + timeDiff);
            console.log('秒杀倒计时，准备进入');
            seckillBox.countdown(killTime, function (event) {
                console.log('秒杀倒计时');
                var format = event.strftime('秒杀倒计时： %D天 %H小时 %M分 %S秒');
                seckillBox.html(format);
                console.log('时间完成后回调事件');
            }).on('finish.countdown', function () {
                //获取秒杀地址，控制逻辑实现，执行秒杀
                seckill.handleSeckillKill(seckillId, seckillBox);
            });
        } else {
            //秒杀开始
            seckill.handleSeckillKill(seckillId, seckillBox);
        }
    },
    //详情页秒杀逻辑
    detail: {

        //详情页初始化
        init: function (params) {
            console.log("!!!!!!detail");
            //手机验证和登录，计时交互
            //规划交互流程
            //在cookie中查找手机号
            var killPhone = $.cookie('killPhone');

            var seckillId = params['seckillId'];
            //验证手机号
            if (!seckill.validatePhone(killPhone)) {
                //绑定phone
                //控制输出
                console.log("!!!!!!killPhoneModal");
                var killPhoneModal = $('#killPhoneModal');
                //modal bootstrap组件，调用方法显示弹出层
                killPhoneModal.modal({
                    show: true,//show pop layer
                    backdrop: 'static',//禁止位置关闭
                    keyboard: false//关闭键盘事件
                });
                $('#killPhoneBtn').click(function () {
                    var inputPhone = $('#killPhoneKey').val();
                    console.log('inputPhone:' + inputPhone);
                    if (seckill.validatePhone(inputPhone)) {
                        //电话写入cookie
                        //  {expires:7,path:'/seckill'}有效期7天,在/seckill路径下有效
                        $.cookie('killPhone', inputPhone, {expires: 7, path: '/seckill'});
                        //刷新页面
                        window.location.reload();
                    } else {
                        //1.隐藏节点，2编辑节点，3显示内容
                        $('#killPhoneMessage').hide().html('<laber class="label label-danger">phone number is wrong</laber>').show(300);
                    }
                });
            }
            //已经登录
            //计时交互
            $.get(seckill.URL.now(), {}, function (result) {
                if (result && result['success']) {
                    var nowTime = result['data'];
                    var localTime = (new Date()).getTime();
                    //时间判断
                    var startTime = params['startTime'];
                    var endTime = params['endTime'];
                    seckill.countdown(seckillId, nowTime, startTime, endTime,localTime);

                } else {
                    console.log("result" + result);
                }
            })

        }
    }

}